import GameObject from "../GameObject";
import GameConst from "../GameConst";
import Utils from "../utils";
import SoundsManager from "../soundmanager";
export default class Menu extends GameObject {

    constructor() {
        super();
        this.animatedButtons = [];
        this.clickables = [];
        this.isMenuActive = false;
        this.lastClickCount = 0;
    }

    show() {
        if (this.sprite) {
            Laya.stage.addChild(this.sprite);
        }
        for (var e = 0; e < this.animatedButtons.length; ++e) {
            this.animatedButtons[e].onShow();
        }
        this.isMenuActive = true;
    }

    update(t) {
        super.update(t);
        var n = this.animatedButtons.length;
        for (var r = 0; r < n; ++r)
            this.animatedButtons[r].update(t);
    }

    onPlayDown() {
        GameConst.level.unpause();
        SoundsManager.instance.playSound("pause");
    };

    restartLevel() {        
        GameConst.levelManager.restartLevel();
    }
    loadMainMenu() {
        GameConst.menuManager.show(GameConst.menuManager.map);
    }

    hide() {
        Utils.removeClip(this.sprite);
        this.isMenuActive = false;
        for (var i = 0; i < this.animatedButtons.length; ++i)
            this.animatedButtons[i].onHide();
    }

    onResize() {};
    onDown(e, t) {};
    checkClick(stx, sty) {
        var t = Laya.timer.currTimer;
        if (t - this.lastClickCount < 1000) {
            return;
        }
        var n = this.clickables.length;
        for (var r = 0; r < n; ++r) {
            var btn = this.clickables[r];
            if (btn && btn.checkClick(stx, sty)) {
                btn.callback && btn.callback.call(this);
                this.lastClickCount = t;
                break
            }
        }
    }
    onUp(x, y) {
        this.checkClick(x, y);
    }
}